package logic

import (
	"cart_srv/cart"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddCartLogic) AddCart(req *types.AddCartRequest) (resp *types.Response, err error) {
	_, err = l.svcCtx.CartSrv.AddCart(l.ctx, &cart.AddCartRequest{
		Name:  req.Name,
		Price: req.Price,
		Num:   req.Num,
	})
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "err.Error()",
			Data: nil,
		}, nil
	}
	if req.Num != 0 {
		return &types.Response{
			Code: -2,
			Msg:  "库存不足",
			Data: nil,
		}, nil
	}

	return
}
